/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __AD7689_H__
#define __AD7689_H__

#ifdef __cplusplus
extern "C" {
#endif

/* USER CODE BEGIN Includes */
#include "main.h"
#include "spi.h"
#include "string.h"
/* USER CODE END Includes */

#define AD7689_CFG_KEEP 0x00 //keep current configuration settings.
#define AD7689_CFG_OVERWRITE 0x01 //overwrite contents of register.
#define AD7689_INCC_UNIPOLAR_GND 0x07 //Unipolar, INx referenced to GND.
#define AD7689_INx_0 0x00
#define AD7689_INx_1 0x01
#define AD7689_INx_2 0x02
#define AD7689_INx_3 0x03
#define AD7689_INx_4 0x04
#define AD7689_INx_5 0x05
#define AD7689_INx_6 0x06
#define AD7689_INx_7 0x07
#define AD7689_BW_FULL 0x01
#define AD7689_REF_IN_2V5 0x00 //Internal reference and temperature sensor enabled. REF = 2.5 V buffered output.
#define AD7689_REF_EXT 0x06 //Use external reference. Internal reference, internal buffer, and temperature sensor disabled.
#define AD7689_REF_EXT_BUFFER 0x07 //Use external reference. Internal buffer enabled. Internal reference and temperature sensor disabled.
#define AD7689_SEQ_DISABLE 0x00 //Disable sequencer.
#define AD7689_RB_DISABLE 0x01 //do not read back contents of configuration.

/* USER CODE BEGIN Private types */
typedef struct __AD7689_CFGTypeDef
{
  uint8_t CFG;
  uint8_t INCC;
  uint8_t INx;
  uint8_t BW;
  uint8_t REF;
  uint8_t SEQ;
  uint8_t RB;
} AD7689_CFGTypeDef;

typedef struct __Temperature_PIDTypeDef
{
  float Kp;
  float Ki;
  float Kd;
  float A[3];
  float sum_error;
  float pre_error;
  float delta_t;
  float output_max;
  float output_min;
} Temperature_PIDTypeDef;
/* USER CODE END Private types */

/* USER CODE BEGIN Prototypes */
AD7689_CFGTypeDef AD7689_Get_Default_CFG(void);
uint16_t AD7689_Generate_CFG(AD7689_CFGTypeDef *pAD7689_CFG);
uint16_t AD7689_Read_SPI(AD7689_CFGTypeDef* pAD7689_CFG);
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi);
float NTC_Temperature_Convert(uint16_t adc_value);
float ChannelLinked_Average_Temperature(uint32_t heat_channel, uint32_t channel_config, float * temp_now);
void Temperature_PID_Init(Temperature_PIDTypeDef* controller, int reset);
float Temperature_PID(Temperature_PIDTypeDef* controller, float error, int constrain_output);
/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif

#endif /* __AD7689_H__ */

